package com.cj.mapper;

import com.cj.pojo.Article;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/*
 * 对文章表进行操作的Mapper接口
 */
@Mapper
public interface ArticleMapper {
    //新增
    @Insert("insert into article(title,content,cover_img,state,category_id,create_user,create_time,update_time) " +
            "values(#{title},#{content},#{coverImg},#{state},#{categoryId},#{createUser},#{createTime},#{updateTime})")
    void add(Article article);

    List<Article> list(Integer userId, Integer categoryId, String state);

    @Select("SELECT * FROM article WHERE category_id = #{categoryId} AND state = #{state} LIMIT #{pageSize} OFFSET #{offset}")
    List<Article> listWithPage(@Param("categoryId") Long categoryId, 
        @Param("state") String state,
        @Param("offset") Integer offset,
        @Param("pageSize") Integer pageSize);

    void batchDelete(List<Long> ids);

    void view(Long id);

    @Select("SELECT SUM(views) FROM article")
    Integer countTotalViews();

    @Select("SELECT SUM(views) FROM article WHERE update_time BETWEEN #{startTime} AND #{endTime}")
    Integer countViewsBetween(@Param("startTime") LocalDateTime startTime, 
        @Param("endTime") LocalDateTime endTime);

    @Select("SELECT id, title, views FROM article ORDER BY views DESC LIMIT #{limit}")
    List<Map<String, Object>> getTopViewedArticles(@Param("limit") int limit);
}
